var codeUuid;
var times=0;
$(function(){
	console.log(localStorage);
	var username = localStorage.getItem("username");
	var userpasswd = localStorage.getItem("userpasswd");
	if(username){
		$('#dlNumber').val(username);
	}
	if(userpasswd){
		$('#dlPassword').val(userpasswd);
	}
	//localMsgToIndex();
	$("#btn").bind('click',function(){
		submitLogin();
	});
	if(username&&userpasswd){
		$('#btn').click();
	}
	
	$(".forget").bind('click',function(){
		window.location=server_url+'forgetPass.do';
	});
	
})
var localMsgToIndex = function(){
	if( null != localStorage.getItem("username") &&null != localStorage.getItem("userpasswd") ){
		var username = localStorage.getItem("username");
		var userpasswd = localStorage.getItem('userpasswd');
		//alert(username+','+userpasswd);return;
		$('#dlNumber').val(username);
		$('#userpasswd').val(userpasswd);
		var loadingMsg = '<div id="loginLoading" class="moskImg1" style="display: block"><img src="/LianniuWeb/img/loadingC.gif"; id="loadingImgC"style="display:block; width:7rem; margin:3rem auto 0;" /></div>';
		$(document.body).append(loadingMsg);
		setTimeout(function(){
			$.ajax({
				type : "post",
				url : server_url+'rest/login.do?username='+username+'&userpasswd='+userpasswd+'&codeUuid='+codeUuid+'&code='+'&times='+times+"&rand="+Math.random(),
				dataType:'json',
				async : false,
				success : function(result) {
					if (result.retCode == '0000') {
						if($('#loginLoading').length>0){
							$('#loginLoading').remove();
						}
						localStorage.setItem(username,1);
						window.location.href=server_url+"buyer/product/index.do";
					}else{
						if($('#loginLoading').length>0){
							$('#loginLoading').remove();
						}
						localStorage.removeItem("username");
						localStorage.removeItem('userpasswd');
					}
				 },
				 error: function(){
					 if($('#loginLoading').length>0){
							$('#loginLoading').remove();
						}
					 easyAlert("服务器异常");
				 }
			   });
		},1000);
	}
}
function refreshCaptcha() {
        codeUuid=uuid();
		$.ajax({
		type : "get",
		url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
		dataType:'json',
		jsonp:'callback',
		async : false,
		success : function(result) {
			if (result.retCode == '0000') {
				data=result.data;
				$('#captchaImage').attr('src',server_url+data);
			}
			else{
				data="";
			}
		}
	});
}
//回车
	document.onkeydown = function (event){ 
		if (event.keyCode==13) //回车键的键值为13 
			submitLogin();
		};
	function submitLogin() {
		var isSupply = params()["supply"];
		times+=1;
		var username = $('#dlNumber').val();
		var userpasswd = $('#dlPassword').val();
		var yzm = $('#yzm').val();
		var jsonObj =[];
		jsonObj.push({name:'username',value:$('#dlNumber').val()});
		jsonObj.push({name:'userpasswd',value:$('#userpasswd').val()});
		jsonObj.push({name:'yzm',value:$('#yzm').val()});
		jsonObj.push({name:'codeUuid',value:codeUuid});
		jsonObj.push({name:'times',value:times});
		if(username==''){
			easyAlert("请输入用户名");
			times-=1;
			return false;
		}
		if(userpasswd==''){
			times-=1;
			easyAlert("请输入密码");
			return false;
		}
		
	   $.ajax({
		type : "post",
		url : server_url+'rest/login.do?isSupply='+isSupply+'&username='+username+'&userpasswd='+userpasswd+'&codeUuid='+codeUuid+'&code='+yzm+'&times=1&ran='+Math.random(),
		dataType:'json',
		async : false,
		success : function(result) {
			if (result.retCode == '0000') {
				token=result.data.token;
				username=result.data.username;
				userid=result.data.userid;
				var userpasswd=result.data.userpasswd;
				$("#token").attr('value',token);
				if(localStorage){
					localStorage.setItem("token",token);
					localStorage.setItem("username",username);
					localStorage.setItem("userid",userid);
					localStorage.setItem("userpasswd",userpasswd);
					localStorage.setItem("user",JSON.stringify(result.data));
					if(result.data.buyer_userid){
						localStorage.setItem("buyer_userid", result.data.buyer_userid);
					}
					if(result.data.seller_userid){
						localStorage.setItem("seller_userid", result.data.seller_userid);
					}
				}
				if(isSupply != null && isSupply != ''){
					window.location.href = server_url + '/skuProductList.do';
				}else{
					localStorage.setItem(username,1);
					localStorage.removeItem('index-location');
					localStorage.setItem("username",username);
					localStorage.setItem("userpasswd",userpasswd);
					window.location.href=server_url+"buyer/product/index.do";
				}
			}
			else if(result.retCode == '1004')
			{	
				easyAlert("验证码错误!");
				return false;
			}
			else if(result.retCode == '1005')
			{
				easyAlert("用户名或者密码错误");
				return false;
			}
			else if(result.retCode == '1002'){
				easyAlert("用户不存在!请注册后再登录");
				return false;
			}
			else if(result.retCode == '1003'){
				easyAlert("用户没有权限");
				return false;
			}
		 },
		 error: function(){
			 easyAlert("服务器异常");
		 }
	   
	   });
	}
	
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}	

function getcodeUuid(){
	codeUuid=uuid();			
	$.ajax({
			type : "get",
			url : server_url+'rest/genCaptchaImage.do?codeUuid='+codeUuid,
			dataType:'json',
			jsonp:'callback',
			async : false,
			success : function(result) {
				if (result.retCode == '0000') {
					data=result.data;
					$('#captchaImage').attr('src',server_url+data);
				}
				else{
					data="";
				}
			}
		});
}
function goForgetHtm(){
	window.location=server_url+"/forgetPass.do";
}
function goResgiter(){
	var isSupply = params()["supply"];
	if (isSupply != null && isSupply != ''){
		window.location.href = server_url + '/supply/register/toRegister.do';
	}else{
		window.location=server_url+"buyerRegister.do";
	}
}